import { Component, OnInit, ViewEncapsulation, Injector } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { AnalysisServiceProxy, AnnualCostDto, WorkshopCostDto } from '@shared/service-proxies/service-proxies';
import G2 from '@antv/g2/build/g2';

@Component({
    templateUrl: './workshop-cost.component.html',
    styleUrls: ['./workshop-cost.componen.less'],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class WorkshopCostComponent extends AppComponentBase implements OnInit {
    tableData: Array<WorkshopCostDto>;
    workshops = {};
    constructor(injector: Injector, private _analysisService: AnalysisServiceProxy) {
        super(injector);
    }

    ngOnInit(): void {
        this.getWorkshops().forEach(el => {
            this.workshops[el.value] = el.displayText;
        });
        console.log(this.workshops);
        this.getData();
    }

    getData() {
        this._analysisService.getWorkshopCost().subscribe(result => {
            this.tableData = result.items;
            this.initChart();
        });
    }

    initChart() {
        let data = [];

        this.tableData.forEach(el => {
            data.push({
                workshop: this.workshops[el.workshop],
                type: '原材料成本',
                value: el['原材料成本']
            });
            data.push({
                workshop: this.workshops[el.workshop],
                type: '辅助材料成本',
                value: el['辅助材料成本']
            });
            data.push({
                workshop: this.workshops[el.workshop],
                type: '标准件成本',
                value: el['标准件成本']
            });
            data.push({
                workshop: this.workshops[el.workshop],
                type: '外协产品成本',
                value: el['外协产品成本']
            });
            data.push({
                workshop: this.workshops[el.workshop],
                type: '工装成本',
                value: el['工装成本']
            });
            data.push({
                workshop: this.workshops[el.workshop],
                type: '燃动成本',
                value: el['燃动成本']
            });
            data.push({
                workshop: this.workshops[el.workshop],
                type: '人工成本',
                value: el['人工成本']
            });
            data.push({
                workshop: this.workshops[el.workshop],
                type: '制造成本',
                value: el['制造成本']
            });
            data.push({
                workshop: this.workshops[el.workshop],
                type: '其他直接成本',
                value: el['其他直接成本']
            });
        });

        let chart = new G2.Chart({
            container: 'c1',
            forceFit: true,
            padding: [50, 80, 80, 50]
        });
        chart.source(data);

        chart
            .interval()
            .position('type*value')
            .color('workshop')
            //.label('value')
            .adjust([
                {
                    type: 'dodge',
                    marginRatio: 1 / 32
                }
            ]);

        chart.render();
    }
}
